package com.itcast.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DataClean {
	public static final String SCRIPT_TAG = "script";
	public static final String STYLE_TAG = "style";
	private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
	private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
	private static final String regEx_html = "<[^>]*>"; // 定义HTML标签的正则表达式
	private static final String regEx_space = "\\s*|\t|\r|\n";// 定义空格回车换行符
	private static final String regEx_punctuation = "\\pP|\\pS";// 去掉中文标点符号
	private static final String regEx_ramark = "<!--[\\s\\S]*?-->";// 去掉中文标点符号



	/**
	 * 去html标签
	 * 
	 * @param text
	 * @return
	 */
	public static String cleanHtml(String text) {
		return text.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(regEx_html, "");
	}
	
	public static String clean(String text) {
		text=cleanScript(text);
		text=cleanStyle(text);
		text=cleanHtml(text);
		text=cleanRemark(text);
		text=cleanEnter(text);
		text=unifySpace(text);
		text=cleanSpace(text);
		text=cleanBdfh(text);
		return text;
	}
	
	
	/**
	 * 去除备注（<!-- -->）
	 * 
	 * @param text
	 * @return
	 */
	public static String cleanRemark(String text) {
		return text.replaceAll(regEx_ramark, "");
	}
	
	/**
	 * 将双换行转为单换行
	 * 
	 * @param text
	 * @return
	 */
	public static String cleanEnter(String text) {
		return text.replaceAll("\n\n", "\n");
	}
	
	/**
	 * 统一空格（全半角的空格统一为半角）
	 * 
	 * @param text
	 * @return
	 */
	public static String unifySpace(String text) {
		return text.replaceAll(" ", " ").replaceAll("　", " ");
	}
	
	/**
	 * 去除格式控制符
	 * 
	 * @param text
	 * @return
	 */
	public static String cleanSpace(String text) {
		return text.replaceAll(regEx_space, "");
	}
	
	/**
	 * 去除标点符号
	 * 
	 * @param text
	 * @return
	 */
	public static String cleanBdfh(String text) {
		return text.replaceAll(regEx_punctuation, "");
	}
	
	/**
	 * 去除Style标签<style></style>
	 * 
	 * @param text
	 * @return
	 */
	public static String cleanStyle(String text) {
		Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
		Matcher m_style = p_style.matcher(text);
		text = m_style.replaceAll(""); // 过滤style标签
		return text;
	}
	
	/**
	 * 去除Style标签<script></script>
	 * 
	 * @param text
	 * @return
	 */
	public static String cleanScript(String text) {
		// return cleanTags(text, SCRIPT_TAG);
		Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
		Matcher m_script = p_script.matcher(text);
		text = m_script.replaceAll(""); // 过滤script标签
		return text;
	}
	
	public static void main(String args[]) throws Exception {
//		ExecutorService pool = Executors.newFixedThreadPool(2);
//		File file = new File("D:/ok/科技/news.qq.com/6b4376e6b35cd05a7fcc86d529618547.html");
//		StringBuffer buf = new StringBuffer();
//		BufferedReader read = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
//		String line;
//		while ((line = read.readLine()) != null) {
//			if (line.trim().length() > 0) {
//				buf.append(line + "\r\n");
//			} else {
//				System.out.println("1" + line);
//			}
//		}
//		
		
//		line = cleanStyle(buf.toString());
//		line = cleanScript(line);
//		line = clean(line);
		// pool.execute(t1);
//		System.out.println(line);
//		pool.shutdown();

	}
}